<?php

namespace app\api\controller;

use app\api\library\Curl;
use app\common\controller\Api;
use app\common\library\WxTemp;
use GuzzleHttp\Client;
use think\Cache;
use think\Exception;

/**
 * 首页接口
 */
class Wen extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function bingxue()
    {
        $key = 'bingxue';
        if (Cache::get($key) == 1) {
            return;
        }
        $id = 4934;
        $client = new Client(['headers' => ['Authorization' => 'Bearer bf4e97c8-f2fe-4d16-9f47-fc7bd2944970']]);
        $url = 'https://api.reservation.inhct.cn/front/scenic/reservation/' . $id;
        try {
            $res = $client->get($url);
            if ($res->getStatusCode() != 200) {
                throw new Exception('请求异常，httpCode=' . $res->getStatusCode());
            }
            $result = json_decode($res->getBody()->getContents(), true);
            if ($result['code'] == 20000) {
                $day = $result['data']['reservationDateList'][7];
                if ($day['week'] == '星期六') {
                    if ($day['reservationSessionList'][0]['limitNum'] == $day['reservationSessionList'][0]['useNum']) {
                        Cache::set($key, 1);
                        echo '没票了';
                        return;
                    }
                    $data = [
                        "reservationId" => $id,
	                    "scenicId"      => $id,
	                    "sessionId"     => $day['reservationSessionList'][0]['sessionId'],
	                    "scheduleId"    => $day['reservationSessionList'][0]['scheduleId'],
                        'visitorIdList' => [
                            8181705,
                            8494818,
                            8509091
                        ]
                    ];
                    $jixu = true;
                    do {
                        if (Curl::posts($client, $data, $key)){
                            Cache::set($key, 1);
                            $jixu = false;
                        }
                        sleep(1);
                    }while($jixu);
                } else {
                    echo '没查到票';
                }
            }
        } catch (\Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function haiyang()
    {

        $key = 'haiyang';
//        Cache::set($key, 0);
        if (Cache::get($key) == 1) {
            echo '不抢了';
            return;
        }
        $id = 1797;
        $client = new Client(['headers' => ['Authorization' => 'Bearer bf4e97c8-f2fe-4d16-9f47-fc7bd2944970']]);
        $url = 'https://api.reservation.inhct.cn/front/scenic/reservation/' . $id;
        try {
            $res = $client->get($url);
            if ($res->getStatusCode() != 200) {
                throw new Exception('请求异常，httpCode=' . $res->getStatusCode());
            }
            $result = json_decode($res->getBody()->getContents(), true);
            if ($result['code'] == 20000) {
                $day = $result['data']['reservationDateList'][0];
                if ($day['week'] == '星期天') {
                    $a = '有票';
                    if ($day['reservationSessionList'][0]['limitNum'] == $day['reservationSessionList'][0]['useNum']) {
//                        Cache::set($key, 1);
                        $a =  '没票了';
//                        return;
                    }
                    $data = [
                        "reservationId" => $id,
                        "scenicId"      => $id,
                        "sessionId"     => $day['reservationSessionList'][0]['sessionId'],
                        "scheduleId"    => $day['reservationSessionList'][0]['scheduleId'],
                        'visitorIdList' => [
                            8181705,
//                            8494818,
//                            8509091
                        ]
                    ];
                    $jixu = true;
                    do {
                        echo $a . '，尝试抢定' . PHP_EOL;
                        if (Curl::posts($client, $data, $key)){
                            Cache::set($key, 1);
                            $jixu = false;
                        }
                        usleep(300000);
                    }while($jixu);
                } else {
                    echo '没查到票';
                }
            }
        } catch (\Exception $e){
            $this->error($e->getMessage());
        }
    }
}
